//
//  MyViewOfScrollView.m
//  SmileRead
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "MyViewOfScrollView.h"
#import "Path.h"

@interface MyViewOfScrollView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;

@property (nonatomic, retain) NSMutableArray *arrayOfItem;

@property (nonatomic, retain) UIPageControl *pageControl;

// 记录图片的下标
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIVisualEffectView *effectView;

@property (nonatomic, retain) UIImageView *titleImageView;

@property (nonatomic, retain) NSArray *arrayOftitle;

@end


@implementation MyViewOfScrollView

- (instancetype)initWithFrame:(CGRect)frame arrayOfImage:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayOftitle = @[@"乐", @"听", @"阅", @""];
        self.arrayOfItem = [NSMutableArray array];
        [self.arrayOfItem addObjectsFromArray:array];
        [self createSubView];
    }
    return self;
}

- (void)dealloc
{
    [_titleImageView release];
    [_effectView release];
    [_titleLabel release];
    [_pageControl release];
    [_arrayOfItem release];
    [_scroll release];
    [super dealloc];
}

- (void)createSubView
{
    self.backgroundColor = [UIColor clearColor];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
//    self.effectView.alpha = 0;
    self.effectView.frame = CGRectMake(0, 0, self.frame.size.width, 50 * heightFit);
    [self addSubview:self.effectView];
    
//    self.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 40, 40)];
//    self.titleImageView.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"musictitle.jpg"]];
//    [self.effectView.contentView addSubview:self.titleImageView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * widthFit, 0, self.effectView.frame.size.width, self.effectView.frame.size.height)];
    self.titleLabel.font = [UIFont systemFontOfSize:30];
    self.titleLabel.text = self.arrayOftitle[0];

    [self.effectView.contentView addSubview:self.titleLabel];
    
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50 * heightFit, self.frame.size.width, self.frame.size.height - 50 * heightFit)];
    self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width * self.arrayOfItem.count, 0);
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scroll];
    for (int i = 0; i < self.arrayOfItem.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height - 20 * heightFit)];
        imageView.userInteractionEnabled = YES;
        NSString *path = [Path pathOfImageWithName:self.arrayOfItem[i]];
        imageView.image = [UIImage imageWithContentsOfFile:path];
        // 为imageView添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Clicked:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        
        [self.scroll addSubview:imageView];
        [imageView release];
    }
    [_scroll release];
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(280 * widthFit, self.scroll.frame.size.height + 30 * heightFit, self.frame.size.width - 280 * widthFit, 20 * heightFit)];
    [self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.pageControl];
    [_pageControl release];
    self.pageControl.numberOfPages = self.arrayOfItem.count - 2;
}

// 点击手势的方法
- (void)Clicked:(UITapGestureRecognizer *)tap
{
    // 将图片下标传过去, 来为首页确定跳转的页面
    NSNumber *number = [NSNumber numberWithInteger:self.index];
    [self.target performSelector:self.action withObject:number];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake((self.arrayOfItem.count - 2) * self.frame.size.width, 0);
    }
    
    if (scrollView.contentOffset.x == (self.arrayOfItem.count - 1) * self.frame.size.width) {
        scrollView.contentOffset = CGPointMake( self.frame.size.width, 0);
    }
    
    self.pageControl.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
    
    self.titleLabel.text = self.arrayOftitle[self.pageControl.currentPage];
    
    self.index = self.pageControl.currentPage;
    // 通知代理执行方法
    [self.delegate sendWithIndex:self.pageControl.currentPage + 1];
}

- (void)pageChange:(UIPageControl *)page
{
    self.scroll.contentOffset = CGPointMake((page.currentPage + 1) * self.frame.size.width, 0);
    
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.arrayOftitle[self.pageControl.currentPage] attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.titleLabel.attributedText = string;
    
    
    // 通知代理执行方法
    self.index = page.currentPage;
    [self.delegate sendWithIndex:page.currentPage + 1];
}



@end
